#include <stdio.h>
#include <stdlib.h>
#include "strExt.h"
#include "../dbg.h"

int indexofTest();
int trimTest();

int main(int argc, char *argv[])
{
    indexofTest();
    trimTest();

    return 0;
}

int indexofTest()
{
    char *s = "Hello string.h strExt.h indexof lastindexof";
    char *t = "Hello";
    int index = indexof(s, t);
    check(index == 0, "except 0, but was %d", index);

    t = "abc";
    index = indexof(s, t);
    check(index == -1, "except -1, but was %d", index);

    t = "str";
    index = indexof(s, t);
    check(index == 6, "except 6, but was %d", index);
    index = lastindexof(s, t);
    check(index == 15, "except 15, but was %d", index);

    t = "indexof";
    index = indexof(s, t);
    check(index == 24, "except 24, but was %d", index);
    index = lastindexof(s, t);
    check(index == 36, "except 36, but was %d", index);
    
    printf("indexofTest ok.\n");

    return 0;

error:
    return -1;
}

int trimTest()
{
    char s[20] = "  hello world  ";
    /* if use a pointer, the programe will be error.
    * 如果使用数组，系统自动把s放在栈中
    * 如果使用指针，s虽然在栈中，但指向代码段
    * c 语言中，你是不能直接修改代码段的，所以会出错！
    */
    //char *s = "  hello world  ";
    ltrim(s);
    check(strcmp(s, "hello world  ") == 0, "ltrim failed");
    rtrim(s);
    check(strcmp(s, "hello world") == 0, "trim failed");
    printf("trimTest ok.\n");
    return 0;

error:
    return -1;
}
